package it.tukano.swingdeco.controls;

import it.tukano.collections.ListExt;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;

/**
 * ButtonGroup extension
 * @author pgi
 */
public class ButtonGroupExt extends ButtonGroup {

    /* Serial version of this class */
    private static final long serialVersionUID = 0;

    /**
     * Instance initializer
     */
    public ButtonGroupExt() {
    }

    public ButtonGroupExt addAll(AbstractButton... buttons) {
        for (int i = 0; i < buttons.length; i++) {
            AbstractButton abstractButton = buttons[i];
            add(abstractButton);
        }
        return this;
    }

    public ButtonGroupExt setSelected(int index) {
        ListExt.<AbstractButton>newListExt(getElements()).get(index).setSelected(true);
        return this;
    }

    public AbstractButton getSelectedButton() {
        Enumeration<AbstractButton> elements = getElements();
        while(elements.hasMoreElements()) {
            AbstractButton button = elements.nextElement();
            if(button.isSelected()) return button;
        }
        return null;
    }
}
